<template>
  <div id="app">
    <!-- 顶部导航栏 -->
    <header class="app-header">
      <div class="header-content">
        <!-- Logo区域 -->
        <div class="logo-area">
          <RouterLink to="/" class="logo-link">
            <h1>樱花中文网</h1>
          </RouterLink>
        </div>
        
        <!-- 主导航菜单 -->
        <nav class="main-nav">
          <ul>
            <!-- 导航项将在首页内部显示 -->
          </ul>
        </nav>
        
        <!-- 用户操作区 -->
        <div class="user-actions">
          <div class="search-container">
            <input type="text" placeholder="搜索小说..." class="search-input" />
            <button class="search-btn">搜索</button>
          </div>
          
          <!-- 用户按钮或头像区域 -->
          <div v-if="!userLoggedIn" class="user-section">
            <button class="user-btn" @click="openUserAuth">用户</button>
          </div>
          
          <!-- 已登录用户头像和设置区域 -->
          <div v-else class="user-section logged-in">
            <!-- 头像轮廓 - 点击可以选择头像图片 -->
            <div class="user-avatar" @click="openAvatarSelector">
              <div class="avatar-placeholder">
                <!-- 显示用户头像或占位符 -->
                <img v-if="currentUser && currentUser.avatar" :src="currentUser.avatar" alt="用户头像" class="avatar-image">
                <span v-else>{{ currentUser ? currentUser.username.charAt(0).toUpperCase() : '?' }}</span>
              </div>
              <!-- 齿轮图标 -->
              <div 
                class="settings-icon" 
                :class="{ 'rotating': isRotating }"
                @click.stop="toggleSettings"
              >
                ⚙️
              </div>
            </div>
            
            <!-- 设置选项卡 -->
            <div v-if="showSettings" class="settings-dropdown">
              <div class="dropdown-item" @click="goToProfile">个人页面</div>
              <div class="dropdown-item" @click="handleLogout">退出登录</div>
              <div class="dropdown-item" @click="handleSwitchAccount">切换账号</div>
            </div>
            
            <!-- 隐藏的文件输入框，用于选择头像图片 -->
            <input
              ref="avatarFileInput"
              type="file"
              accept="image/*"
              style="display: none"
              @change="handleAvatarChange"
            >
          </div>
        </div>
      </div>
    </header>
    
    <!-- 主内容区域 -->
    <main class="main-content">
      <RouterView />
    </main>
    
    <!-- 页脚 -->
    <footer class="app-footer">
      <div class="footer-content">
        <p>© 2023 樱花中文网 版权所有</p>
        <div class="footer-links">
          <a href="#">关于我们</a>
          <a href="#">联系我们</a>
          <a href="#">使用条款</a>
          <a href="#">隐私政策</a>
        </div>
      </div>
    </footer>
  </div>
</template>

<script>
import { useRouter, RouterLink } from 'vue-router'
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { isLoggedIn, logoutUser, switchAccount, getCurrentUser, saveUserAvatar } from './utils/userService.js'

export default {
  name: 'App',
  setup() {
    const router = useRouter()
    const showSettings = ref(false)
    const isRotating = ref(false)
    const currentUser = ref(null)
    const avatarFileInput = ref(null)
    
    // 计算属性：检查用户是否已登录
    const userLoggedIn = computed(() => {
      return isLoggedIn()
    })
    
    // 获取当前用户信息
    const loadCurrentUser = () => {
      currentUser.value = getCurrentUser()
    }
    
    // 打开用户认证页面
    const openUserAuth = () => {
      router.push('/user-auth')
    }
    
    // 打开头像选择对话框
    const openAvatarSelector = () => {
      if (avatarFileInput.value) {
        avatarFileInput.value.click()
      }
    }
    
    // 处理头像选择
    const handleAvatarChange = (event) => {
      const file = event.target.files[0]
      if (file) {
        const reader = new FileReader()
        reader.onload = (e) => {
          // 保存头像URL到localStorage
          saveUserAvatar(e.target.result)
          // 重新加载用户信息以更新UI
          loadCurrentUser()
        }
        reader.readAsDataURL(file)
        // 重置文件输入，允许选择相同的文件
        event.target.value = ''
      }
    }
    
    // 切换设置选项卡显示/隐藏
    const toggleSettings = () => {
      // 开始转动动画
      isRotating.value = true
      
      // 短暂延迟后显示/隐藏选项卡
      setTimeout(() => {
        showSettings.value = !showSettings.value
        
        // 动画结束后停止转动
        setTimeout(() => {
          isRotating.value = false
        }, 500)
      }, 300)
    }
    
    // 退出登录
    const handleLogout = () => {
      logoutUser()
      showSettings.value = false
      // 刷新页面以更新UI
      window.location.reload()
    }
    
    // 切换账号
    const handleSwitchAccount = () => {
      switchAccount()
      showSettings.value = false
      // 跳转到登录页面
      router.push('/user-auth')
    }
    
    // 跳转到个人页面
    const goToProfile = () => {
      showSettings.value = false
      router.push('/profile')
    }
    
    // 点击页面其他区域关闭设置选项卡
    const handleClickOutside = (event) => {
      if (showSettings.value) {
        const userSection = document.querySelector('.user-section.logged-in')
        if (userSection && !userSection.contains(event.target)) {
          showSettings.value = false
        }
      }
    }
    
    // 组件挂载时添加点击事件监听
    onMounted(() => {
      document.addEventListener('click', handleClickOutside)
      // 加载当前用户信息
      loadCurrentUser()
    })
    
    // 组件卸载时移除点击事件监听
    onUnmounted(() => {
      document.removeEventListener('click', handleClickOutside)
    })
    
    return {
      RouterLink,
      userLoggedIn,
      showSettings,
      isRotating,
      currentUser,
      avatarFileInput,
      openUserAuth,
      toggleSettings,
      handleLogout,
      handleSwitchAccount,
      goToProfile,
      openAvatarSelector,
      handleAvatarChange
    }
  }
}
</script>

<style scoped>
/* 全局样式 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  line-height: 1.6;
  color: #333;
}

/* 顶部导航栏样式 */
.app-header {
  background-color: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 80px;
}

/* Logo样式 */
.logo-area h1 {
  font-size: 28px;
  font-weight: bold;
  color: #e81742;
  margin: 0;
}

.logo-link {
  text-decoration: none;
  display: inline-block;
}

/* 主导航菜单样式 */
.main-nav ul {
  display: flex;
  list-style: none;
  gap: 30px;
}

.nav-link {
  color: #333;
  text-decoration: none;
  font-size: 16px;
  font-weight: 500;
  padding: 8px 12px;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.nav-link:hover {
  color: #e81742;
  background-color: rgba(232, 23, 66, 0.05);
}

/* 用户操作区样式 */
.user-actions {
  display: flex;
  align-items: center;
  gap: 20px;
}

.search-container {
  position: relative;
  display: flex;
  align-items: center;
}

.search-input {
  padding: 8px 16px;
  border: 1px solid #ddd;
  border-radius: 20px 0 0 20px;
  outline: none;
  font-size: 14px;
  width: 200px;
  transition: all 0.3s ease;
}

.search-input:focus {
  border-color: #e81742;
  width: 250px;
}

.search-btn {
  padding: 8px 16px;
  background-color: #e81742;
  color: white;
  border: none;
  border-radius: 0 20px 20px 0;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.3s ease;
}

.search-btn:hover {
  background-color: #c71541;
}

/* 用户按钮样式 */
.user-btn {
  padding: 8px 16px;
  background-color: #e81742;
  color: white;
  border: none;
  border-radius: 4px;
  text-decoration: none;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s ease;
  display: inline-block;
}

.user-btn:hover {
  background-color: #c71541;
  transform: translateY(-1px);
}

/* 用户头像样式 */
.user-avatar {
  position: relative;
  width: 40px;
  height: 40px;
  cursor: pointer;
}

.avatar-placeholder {
  width: 100%;
  height: 100%;
  background-color: #f0f0f0;
  border-radius: 50%;
  border: 2px solid #e81742;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  color: #666;
  overflow: hidden;
}

/* 头像图片样式 */
.avatar-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
}

/* 齿轮图标样式 */
.settings-icon {
  position: absolute;
  bottom: -8px;
  right: -8px;
  width: 24px;
  height: 24px;
  background-color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  transition: transform 0.3s ease;
}

/* 齿轮转动动画 */
.settings-icon.rotating {
  animation: rotate 1s linear;
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

/* 设置选项卡样式 */
.settings-dropdown {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 10px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  overflow: hidden;
  z-index: 1001;
  min-width: 120px;
}

.dropdown-item {
  padding: 12px 16px;
  text-align: left;
  cursor: pointer;
  transition: background-color 0.3s ease;
  font-size: 14px;
  color: #333;
  border-bottom: 1px solid #f0f0f0;
}

.dropdown-item:last-child {
  border-bottom: none;
}

.dropdown-item:hover {
  background-color: #f8f8f8;
  color: #e81742;
}

/* 主内容区域样式 */
.main-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 100px 20px 60px;
  min-height: calc(100vh - 160px);
}

/* 页脚样式 */
.app-footer {
  background-color: #f8f8f8;
  border-top: 1px solid #e0e0e0;
  padding: 30px 0;
}

.footer-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  text-align: center;
}

.footer-content p {
  color: #666;
  font-size: 14px;
  margin-bottom: 15px;
}

.footer-links {
  display: flex;
  justify-content: center;
  gap: 20px;
  flex-wrap: wrap;
}

.footer-links a {
  color: #666;
  text-decoration: none;
  font-size: 14px;
  transition: color 0.3s ease;
}

.footer-links a:hover {
  color: #e81742;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .header-content {
    padding: 0 15px;
  }
  
  .main-nav ul {
    gap: 20px;
  }
  
  .search-input {
    width: 150px;
  }
  
  .search-input:focus {
    width: 200px;
  }
}

@media (max-width: 768px) {
  .header-content {
    flex-direction: column;
    height: auto;
    padding: 15px;
    gap: 15px;
  }
  
  .main-nav ul {
    gap: 15px;
    flex-wrap: wrap;
    justify-content: center;
  }
  
  .user-actions {
    width: 100%;
    justify-content: center;
    gap: 15px;
  }
  
  .search-input {
    width: 100%;
    max-width: 200px;
  }
  
  .search-input:focus {
    width: 100%;
    max-width: 250px;
  }
  
  .main-content {
    padding: 180px 15px 40px;
  }
  
  .footer-links {
    gap: 15px;
  }
}

@media (max-width: 480px) {
  .logo-area h1 {
    font-size: 24px;
  }
  
  .main-nav ul {
    flex-direction: column;
    align-items: center;
    gap: 10px;
  }
  
  .user-actions {
    flex-direction: column;
    gap: 10px;
  }
  
  .search-container {
    width: 100%;
    justify-content: center;
  }
  
  .search-input {
    width: 100%;
    max-width: 250px;
  }
}
</style>
